package com.xzqwjw.taskmanager.activity;

import com.xzqwjw.taskmanager.R;
import com.leaderinfo.utility.Utility;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.app.Activity;
import android.content.Intent;

public class ActivityCondition extends Activity {

  private String strDBFile = "";

  private EditText etName;

  private Spinner spAgeMin;
  private Spinner spAgeMax;
  private Spinner spGOVLevel;
  private Spinner spJobLevel;

  private RadioGroup rgSex;
  private RadioButton rbSex;
  private CheckBox cbNation;
  private CheckBox cbIsParty;

  private Button btnQuery;

  private ArrayAdapter<String> adpAgeMin;
  private static final String[] aryAgeMin = new String[60];

  private ArrayAdapter<String> adpAgeMax;
  private static final String[] aryAgeMax = new String[60];

  private ArrayAdapter<String> adpGOVLevel;
  private static final String[] aryGOVLevel = new String[8];
  private ArrayAdapter<String> adpJobLevel;
  private static final String[] aryJobLevel = new String[5];

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_condition);

    Intent objIT = getIntent();
    strDBFile = objIT.getStringExtra("DBFilePath");

    int i;

    for (i = 15; i < 75; i++) {
      aryAgeMin[i - 15] = i + "";
      aryAgeMax[i - 15] = i + "";
      // aryYears[i - 15] = i - 15 + "";
    }

    aryGOVLevel[0] = "�����ô�����";
    aryGOVLevel[1] = "����ʮ��(Ա��)";
    aryGOVLevel[2] = "����ż�(��Ա��)";
    aryGOVLevel[3] = "����˼�(���Ƽ�)";
    aryGOVLevel[4] = "�����߼�(���Ƽ�)";
    aryGOVLevel[5] = "��������(������)";
    aryGOVLevel[6] = "�����弶(������)";
    aryGOVLevel[7] = "�����ļ�(���ּ�)";

    aryJobLevel[0] = "�����ô�����";
    aryJobLevel[1] = "���ڼ�";
    aryJobLevel[2] = "�����ڼ�";
    aryJobLevel[3] = "��ʦ��";
    aryJobLevel[4] = "����";

    etName = (EditText) findViewById(R.id.etName);

    // ����Ĭ��ֵ

    spAgeMin = (Spinner) findViewById(R.id.spAgeMin);
    adpAgeMin = new ArrayAdapter<String>(this, R.layout.view_spinner, aryAgeMin);
    adpAgeMin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spAgeMin.setAdapter(adpAgeMin);
    spAgeMin.setSelection(25, true);
    spAgeMin.setVisibility(View.VISIBLE);

    spAgeMax = (Spinner) findViewById(R.id.spAgeMax);
    adpAgeMax = new ArrayAdapter<String>(this, R.layout.view_spinner, aryAgeMax);
    adpAgeMax.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spAgeMax.setAdapter(adpAgeMax);
    spAgeMax.setSelection(45, true);
    spAgeMax.setVisibility(View.VISIBLE);

    // spYears = (Spinner) findViewById(R.id.spYears);
    // adpYears = new ArrayAdapter<String>(this, R.layout.view_spinner,
    // aryYears);
    // adpYears.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // spYears.setAdapter(adpYears);
    // spYears.setVisibility(View.VISIBLE);

    spGOVLevel = (Spinner) findViewById(R.id.spGOVLevel);
    adpGOVLevel = new ArrayAdapter<String>(this, R.layout.view_spinner, aryGOVLevel);
    adpGOVLevel.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spGOVLevel.setAdapter(adpGOVLevel);
    spGOVLevel.setVisibility(View.VISIBLE);

    spJobLevel = (Spinner) findViewById(R.id.spJobLevel);
    adpJobLevel = new ArrayAdapter<String>(this, R.layout.view_spinner, aryJobLevel);
    adpJobLevel.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spJobLevel.setAdapter(adpJobLevel);
    spJobLevel.setVisibility(View.VISIBLE);

    rgSex = (RadioGroup) this.findViewById(R.id.rgSex);
    int intRGID = rgSex.getCheckedRadioButtonId();
    rbSex = (RadioButton) this.findViewById(intRGID);

    cbNation = (CheckBox) this.findViewById(R.id.cbNation);
    cbIsParty = (CheckBox) this.findViewById(R.id.cbIsParty);

    btnQuery = (Button) this.findViewById(R.id.btnQuery);
    btnQuery.setOnClickListener(new OnClickListener());

  }

  class OnClickListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {
      String strWhere = "";
      String strWhereName = "";
      String strWhereAge = setConditionAge();
      String strWhereGOVLevel = setConditionGOVLevel();
      String strWhereJobLevel = setConditionJobLevel();
      String strWhereOther = setConditionOther();

      // �����������������������Ϊ�գ�������������ʧЧ
      String strName = etName.getText().toString().trim();
      if (strName.length() > 0) {
        strWhereName = "tbasic.realname LIKE '%" + strName + "%'";
      }

      if (strWhereName.equals("")) {
        strWhere = "WHERE " + strWhereAge + strWhereGOVLevel + strWhereJobLevel + strWhereOther;
      } else {
        strWhere = "WHERE " + strWhereName + " AND " + strWhereAge + strWhereGOVLevel + strWhereJobLevel
            + strWhereOther;
      }

      Intent objIT = new Intent(ActivityCondition.this, ActivityQueryResult.class);
      objIT.putExtra("DBFilePath", strDBFile);
      objIT.putExtra("QueryKey", 3);
      objIT.putExtra("Where", strWhere);
      startActivity(objIT);
    }

  }

  // ������������
  private String setConditionAge() {
    String strWhereAge = "";
    String strAgeMin, strAgeMax;
    int intAgeMin, intAgeMax, intAgeTemp;

    strAgeMin = spAgeMin.getSelectedItem().toString();
    strAgeMax = spAgeMax.getSelectedItem().toString();

    intAgeMin = Integer.parseInt(strAgeMin);
    intAgeMax = Integer.parseInt(strAgeMax);

    if (intAgeMin > intAgeMax) {
      intAgeTemp = intAgeMin;
      intAgeMin = intAgeMax;
      intAgeMax = intAgeTemp;
    }
    for (int i = 0; i <= intAgeMax - intAgeMin; i++) {
      strWhereAge += " OR tbasic.birthday LIKE '" + Utility.getBirthdayFromAge(intAgeMin + i + "") + "%'";
    }

    // ע������Ҫȥ����ͷ��"OR "3���ַ�
    // ��󷵻�����(tbasic.birthday LIKE '1978&' OR
    // tbasic.birthday LIKE '1979&')�������ַ���
    strWhereAge = "(" + strWhereAge.substring(3) + ")";

    return strWhereAge;
  }

  // ����ְ�񼶱�����
  private String setConditionGOVLevel() {
    String strGOVLevel = spGOVLevel.getSelectedItem().toString();
    if (strGOVLevel.equals("�����ô�����"))
      return "";
    else
      return " AND tunit.appointment_category='" + strGOVLevel + "'";
  }

  // ����ְ�Ƽ�������
  private String setConditionJobLevel() {
    String strJobLevel = spJobLevel.getSelectedItem().toString();
    if (strJobLevel.equals("�����ô�����"))
      return "";
    else
      return " AND tunit.work_content='" + strJobLevel + "'";
  }

  private String GetGOVLevelByID(String strGOVLevel) {
    String strReturn = "";
    if (strGOVLevel == "����ʮ��(Ա��)")
      strReturn = "1";
    else if (strGOVLevel.equals("����ż�(��Ա��)"))
      strReturn = "2";
    else if (strGOVLevel.equals("����˼�(���Ƽ�)"))
      strReturn = "3";
    else if (strGOVLevel.equals("�����߼�(���Ƽ�)"))
      strReturn = "4";
    else if (strGOVLevel.equals("��������(������)"))
      strReturn = "5";
    else if (strGOVLevel.equals("�����弶(������)"))
      strReturn = "6";
    else if (strGOVLevel.equals("�����ļ�(���ּ�)"))
      strReturn = "7";
    return strReturn;
  }

  private int GetJobLevelByID(String strJobLevel) {
    int strReturn = 0;
    if (strJobLevel.equals("���ڼ�"))
      strReturn = 1;
    else if (strJobLevel.equals("�����ڼ�"))
      strReturn = 2;
    else if (strJobLevel.equals("��ʦ��"))
      strReturn = 3;
    else if (strJobLevel.equals("����"))
      strReturn = 4;
    return strReturn;
  }

  // �����Ա��Ƿ�Ա�����������
  // ��󷵻����� " AND tbasic.sex='Ů'"�������ַ���
  private String setConditionOther() {
    String strWhere = "";
    // �����Ա�
    String strSex = rbSex.getText().toString();
    for (int i = 0; i < rgSex.getChildCount(); i++) {
      rbSex = (RadioButton) rgSex.getChildAt(i);
      if (rbSex.isChecked()) {
        strSex = (String) rbSex.getText();
        break;
      }
    }

    if (strSex.equals("��")) {
      strWhere += " AND tbasic.sex='��'";
    } else if (strSex.equals("Ů")) {
      strWhere += " AND tbasic.sex='Ů'";
    } else {
      strWhere += "";
    }
    // ��������
    if (cbNation.isChecked()) {
      strWhere += " AND tbasic.nation<>'����'";
    }
    // �����Ƿ��й���Ա
    if (cbIsParty.isChecked()) {
      strWhere += " AND tbasic.party1_name<>'�й���Ա'";
    }

    return strWhere;
  }

}
